package org.com.cis.cf.measure;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import org.com.cis.cf.util.SortByPredicted;
import org.com.cis.cf.util.TestPoint;

public class AUCScorer extends MetricScorer { 
	
	public double score(ArrayList<TestPoint> rateList, int posNum) 
	{
		double score = 0.0;

		int length = rateList.size(), i;
		int negNum = length - posNum;
		
		@SuppressWarnings("unchecked")
		Comparator<TestPoint> sortByPredicted = new SortByPredicted();
		Collections.sort(rateList, sortByPredicted);

		int rate, neg = 0;
		for( i = 1; i <= length; ++i){
			rate = rateList.get(i-1).getTrue();
			if(rate == 1) {
				score += (negNum - neg);
			} else {
				neg += 1;
			}
		}
		score /= (posNum * negNum);
		return score;
	}
}
